<?php

ini_set('include_path', '../');

require_once("lib/exception/duplicate_instance_exception.php");
require_once("lib/exception/internal_error_exception.php");
require_once("lib/exception/instance_not_found_exception.php");

require_once("util/configuration_parametters.php");

require_once("model/autor/to/autor_to.php");
require_once("model/autor/dao/autor_dao_factory.php");

class Autor_dao_test {

	private $dao;
	
	public function __construct() {}	
	
	public function before_test() {
		try {
			$this->dao = Autor_dao_factory::singleton()->get_dao();
		}
		catch (Exception $e) {
			throw $e; 
		}
	}
	
	public function run_test() {		
		try {			
			$autor_to = new Autor_to('xxxxxxxxxxxx','password','email');

			//create
			$autor_to = $this->dao->create($autor_to);	
			echo 'create article: ' . $autor_to->to_string() . '<br/>';

			//create existent login 
			try {
				$this->dao->create($autor_to);
				throw new Exception('create duplicate');
			}
			catch (Duplicate_instance_exception $e) {
				echo $e->getMessage() . '</br>';
			}
			
			//exist inexistent 
			if (!$this->dao->exist("yyyyyyyyy"))
				echo 'No exist description 9999<br/>';
			else 
				throw new Exception("Error, exist description inexistent");
			
			//exist existent 
			$this->dao->exist($autor_to->get_login());
			echo 'exist autor: ' . $autor_to->to_string() . '<br/>';
			
			//find inexistent 
			try {
				$this->dao->find_by_login_autor("yyyyyyyyy");
				throw new Exception('Found inexistent');
			}
			catch (Instance_not_found_exception $e) {
				echo $e->getMessage() . '</br>';
			}
			
			//find existent 
			$aux_autor_to = $this->dao->find_by_login_autor($autor_to->get_login());	
				if ($autor_to->equals($aux_autor_to)) 
					echo 'found autor: ' . $autor_to->to_string() . '<br/>';
				else
					echo 'not equal autor inserted' . $aux_autor_to->to_string();
			
			//update password			
			$this->dao->update_password($autor_to->get_login(), "password2");
			$aux_autor_to = $this->dao->find_by_login_autor($autor_to->get_login());
			echo 'update autor password: ' . $aux_autor_to->to_string() . '<br/>';
			
			//update autor
			$autor_to->set_email('email2');			
			$this->dao->update($autor_to);
			$aux_autor_to = $this->dao->find_by_login_autor($autor_to->get_login());
			echo 'update autor: ' . $aux_autor_to->to_string() . '<br/>';

			//remove inexistent 	
			try {
				$this->dao->remove("yyyyyyyyy");
				throw new Exception('Found inexistent');
			}
			catch (Instance_not_found_exception $e) {
				echo $e->getMessage() . '</br>';
			}
			
			//remove existent 
			$this->dao->remove($autor_to->get_login());
			echo 'remove autor: ' . $autor_to->to_string() . '<br/>';
					
		}
		catch (Duplicate_instance_exception $e) {
			throw $e;
		}
		catch (Internal_error_exception $e) {
			throw $e;
		}
		catch (Instance_not_found_exception $e) {
			throw $e;
		}
		catch (Exception $e) {
			throw $e;
		}
	}
	
	public function after_test() {}
}

//test
$test = new Autor_dao_test();

try {
	$test->before_test();
	$test->run_test();
	$test->after_test();
}
catch (Exception $e) {
	echo $e->getMessage();
}

?>